package net.nettytlv.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import net.nettytlv.domain.Packet;

/**
 * 编码器
 */
@Sharable
public class Encoder extends MessageToByteEncoder<Packet> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Packet packet,
                          ByteBuf out) {
        out.writeByte(packet.getType());
        out.writeShort(packet.getLength());
        out.writeBytes(packet.getValue());
    }
}
